// pages/shop/shop.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    frameList: [],
    driveList: [],
    showList: [],
    topShow: true,
    listNum: -1,
    showImage:"",
    showImageShow:false,
    userInfo:''
  },
  frameCh() {
    this.setData({
      topShow: true,
      showList: this.data.frameList,
      listNum: 0,
      listNum: -1,
    })
  },
  driveCh() {
    this.setData({
      topShow: false,
      showList: this.data.driveList,
      listNum: -1,
      showImageShow:false
    })
  },
  listNumCh(data) {
    if(this.data.topShow){
      if(data.currentTarget.dataset.index==this.data.listNum){
        this.setData({
          listNum: -1,
          showImageShow:false
        })
      }else{
        this.setData({
          listNum: data.currentTarget.dataset.index,
          showImageShow:true,
          showImage:this.data.frameList[data.currentTarget.dataset.index].image
        })
      }
    }else{
      if(data.currentTarget.dataset.index==this.data.listNum){
        this.setData({
          listNum: -1,
        })
      }else{
        this.setData({
          listNum: data.currentTarget.dataset.index,
        })
      }
    }
  },
  toshopGive(data) {
    let num=data.currentTarget.dataset.index
    if (this.data.showList[num].diamond) {
      wx.navigateTo({
        url: '/pages/shopGive/shopGive?goodsId=' + this.data.showList[num].goodsId,
      })
    } else {
      wx.showToast({
        title: '仅限活动获得，无法赠送',
        icon: 'none'
      })
    }

  },
  toshopBag() {
    wx.navigateTo({
      url: '/pages/shopBag/shopBag',
    })
  },
  buy(data) {
    let num=data.currentTarget.dataset.index
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&goodId=" +
      this.data.showList[num].goodsId +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      goodId: this.data.showList[num].goodsId,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/goods/buygoods",
      data: params
    }).then((res) => {
      console.log(res.data.result),
        wx.showToast({
          title: res.data.text,
          icon: 'none'
        })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/goods/index",
      data: params
    }).then((res) => {
      console.log(res.data.result),
        this.setData({
          showList: res.data.result.head,
          frameList: res.data.result.head,
          driveList: res.data.result.drive
        })
    })
    axios.axios({
      url: "/user/userInfo",
      data: params
    }).then((res) => {
      console.log(res.data.result),
        this.setData({
          userInfo:res.data.result.user
        })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})